package com.tinckay.common.handler;

import com.tinckay.common.util.ShiroUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Author: Jean
 * @Desc: 实体公共字段填充策略配置
 * @Date: 2018/10/23 11:55
 */
@Component
public class MetaObjectHandler implements com.baomidou.mybatisplus.core.handlers.MetaObjectHandler {

    private static final Logger logger = LoggerFactory.getLogger(MetaObjectHandler.class);

    private static final String DATATIME_SETTER = "dataTime";
    private static final String CREATOR = "creator";

    /**
     * insert填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.hasSetter(DATATIME_SETTER)) {
            metaObject.setValue("dataTime", LocalDateTime.now());
        }
        if (metaObject.hasSetter(CREATOR)) {
            metaObject.setValue("creator", ShiroUtils.getCurrentUser().getName());
        }
    }

    /**
     * update填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        //logger.info("start update fill ....");
    }

}
